package export

import (
	"fmt"
	"github.com/xuri/excelize/v2"
	"internal/api/types"
	"internal/store"
	"internal/store/mysql"
	"internal/util"
)

func GetUserInfoExport(f *excelize.File, req *types.UserInfoExportReq) error {
	var ExcelFileSheetName string = "用户信息表"
	f.SetSheetName("Sheet1", ExcelFileSheetName)
	// 设置文字居中
	HeaderStyle, err := f.NewStyle(&excelize.Style{
		Alignment: &excelize.Alignment{
			Horizontal: "center",
			Vertical:   "center",
		},
		Border: []excelize.Border{
			{Type: "left", Color: "000000", Style: 1},
			{Type: "right", Color: "000000", Style: 1},
			{Type: "top", Color: "000000", Style: 1},
			{Type: "bottom", Color: "000000", Style: 1},
		},
	})
	// 合并A1到C1的单元格
	f.MergeCell(ExcelFileSheetName, "A1", "F1")
	f.SetCellValue(ExcelFileSheetName, "A1", "用户信息表")
	f.SetCellStyle(ExcelFileSheetName, "A1", "A1", HeaderStyle)
	TitleStyle, err := f.NewStyle(&excelize.Style{
		Alignment: &excelize.Alignment{
			Horizontal: "center",
			Vertical:   "center",
		},
		Fill: excelize.Fill{
			Type:    "pattern",
			Pattern: 1,
			Color:   []string{"#C2C2C2"},
		},
		Border: []excelize.Border{
			{Type: "left", Color: "000000", Style: 1},
			{Type: "right", Color: "000000", Style: 1},
			{Type: "top", Color: "000000", Style: 1},
			{Type: "bottom", Color: "000000", Style: 1},
		},
	})
	// 设置标题
	titles := []string{"用户id", "用户名", "用户密码", "登录时间", "编辑时间", "注册时间"}
	for i, v := range titles {
		cell := fmt.Sprintf("%c%d", 'A'+i, 2)
		// 设置单元格的样式
		if err = f.SetCellStyle(ExcelFileSheetName, cell, cell, TitleStyle); err != nil {
			util.LogSrv.Error("[ExcelModule] fill color err=%v", err)
			return err
		}
		// 设置单元格的值
		f.SetCellValue(ExcelFileSheetName, cell, v)
		// 设置单元格宽度
		f.SetColWidth(ExcelFileSheetName, fmt.Sprintf("%c", 'A'+i), fmt.Sprintf("%c", 'A'+i), 20)
	}
	// 获取用户信息数据
	data, err := GetUserInfoList(req.Username)
	if err != nil {
		return err
	}
	// 数据写入excel
	for i, row := range data {
		if err := WriteIntoExcelFile(f, ExcelFileSheetName, i, &row); err != nil {
			util.LogSrv.Error("[ExcelModule] WriteIntoExcelFile err=%v", err)
			return err
		}
	}
	return nil
}

// GetUserInfoList 获取用户信息列表
func GetUserInfoList(Username string) (value []types.UserInfo, err error) {
	var userList []store.FkUser
	err = mysql.GetConn().Where("f_name LIKE ?", Username+"%").Find(&userList).Error
	if err != nil {
		util.LogSrv.Error("fk_user query f_name = %s ,err=%v", Username, err)
		return value, err
	}
	for _, v := range userList {
		userInfo := types.UserInfo{
			UserId:       v.UserId,
			UserName:     v.UserName,
			Password:     v.Password,
			LoginTime:    v.LoginTime.Format("2006-01-02 15:04:05"),
			RegisterTime: v.RegisterTime.Format("2006-01-02 15:04:05"),
			EditTime:     v.EditTime.Format("2006-01-02 15:04:05"),
		}
		value = append(value, userInfo)
	}
	return value, nil
}

// WriteIntoExcelFile 写入excel文件
func WriteIntoExcelFile(f *excelize.File, sheetName string, row int, data *types.UserInfo) (err error) {
	// 内容风格
	ContentStyle, err := f.NewStyle(&excelize.Style{
		Alignment: &excelize.Alignment{
			Horizontal: "center",
			Vertical:   "center",
		},
		Border: []excelize.Border{
			{Type: "left", Color: "000000", Style: 1},
			{Type: "right", Color: "000000", Style: 1},
			{Type: "top", Color: "000000", Style: 1},
			{Type: "bottom", Color: "000000", Style: 1},
		},
	})
	cols := []string{"A", "B", "C", "D", "E", "F"}
	values := []interface{}{data.UserId, data.UserName, data.Password, data.LoginTime, data.RegisterTime, data.EditTime}
	for i, col := range cols {
		cell := fmt.Sprintf("%s%d", col, row+3)
		err := f.SetCellValue(sheetName, cell, values[i])
		f.SetCellStyle(sheetName, cell, cell, ContentStyle)
		if err != nil {
			return err
		}
	}
	return nil
}
